package edu.msse.signup.ui;

import java.util.Date;
import java.util.Map;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

import edu.msse.signup.domain.Position;
import edu.msse.signup.domain.TimeSlot;
import edu.msse.signup.domain.Users;
import edu.msse.signup.service.PositionService;
import edu.msse.signup.service.TimeSlotService;
import edu.msse.signup.service.UsersService;
import edu.msse.signup.service.VolunteerService;
public class confirmationHandler {
	
	
	private ApplicationService	applicationService;
	private PositionService positionService;
	private TimeSlotService	timeSlotService;
	private loginHandler loginHandler;
	private UsersService usersService;
	private VolunteerService volunteerService;
	
	private String eventName;
	private String slotId;
	private String slotType;
	private String positionName;
	private Date startTime;
	private Date endTime;
	
	
	public String confirmVolunteer() throws Exception {

		try{
		Map requestMap = FacesContext.getCurrentInstance().getExternalContext().getRequestMap();
		PositionTimeSlotDetail pos = (PositionTimeSlotDetail)requestMap.get("pos");
		eventName = pos.getEventId();//(String)parameterMap.get("eventName");
		slotId = pos.getId(); //(String)parameterMap.get("slotId");
		slotType = pos.getSlotType(); //(String)parameterMap.get("slotType");
		startTime = pos.getStartTime();//(String)parameterMap.get("startTime");
		endTime = pos.getEndTime();//(String)parameterMap.get("endTime");
		positionName = pos.getName(); //(String)parameterMap.get("posName");
		
		
		if (slotType.equals("P")){
			Position 	position  	= (Position)positionService.getPosition(Integer.parseInt(slotId));
			Users user = (Users)usersService.findUsersByEmailAddress(loginHandler.getUsername()).get(0);
			volunteerService.fillPositionTimeSlot(position,null, user);
		}
		else if (slotType.equals("T")){
			TimeSlot 	timeslot	= timeSlotService.getTimeSlot(Integer.parseInt(slotId));
			Users user = (Users)usersService.findUsersByEmailAddress(loginHandler.getUsername()).get(0);
			volunteerService.fillPositionTimeSlot(null,timeslot, user);
		}
		
		}
		catch(Exception e){
			applicationService.addErrorMessage(e.getMessage());	
		}
		return "confirmVolunteer";
	}
	
	
	public String getEventName() {
		return eventName;
	}
	public void setEventId(String eventName) {
		this.eventName = eventName;
	}
	public String getSlotId() {
		return slotId;
	}
	public void setSlotId(String slotId) {
		this.slotId = slotId;
	}
	public String getSlotType() {
		return slotType;
	}
	public void setSlotType(String slotType) {
		this.slotType = slotType;
	}
	public String getPositionName() {
		return positionName;
	}
	public void setPositionName(String posName) {
		this.positionName = posName;
	}
	public Date getStartTime() {
		return startTime;
	}
	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}
	public Date getEndTime() {
		return endTime;
	}
	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}
	
	public void setApplicationService(ApplicationService applicationService) {
		this.applicationService = applicationService;
	}

	public void setVolunteerService(VolunteerService volunteerService) {
		this.volunteerService = volunteerService;
	}

	public PositionService getPositionService() {
		return positionService;
	}


	public void setPositionService(PositionService positionService) {
		this.positionService = positionService;
	}


	public TimeSlotService getTimeSlotService() {
		return timeSlotService;
	}


	public void setTimeSlotService(TimeSlotService timeSlotService) {
		this.timeSlotService = timeSlotService;
	}


	public loginHandler getLoginHandler() {
		return loginHandler;
	}


	public void setLoginHandler(loginHandler loginHandler) {
		this.loginHandler = loginHandler;
	}


	public UsersService getUsersService() {
		return usersService;
	}


	public void setUsersService(UsersService usersService) {
		this.usersService = usersService;
	}


	public ApplicationService getApplicationService() {
		return applicationService;
	}


	public VolunteerService getVolunteerService() {
		return volunteerService;
	}

}
